/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import com.uca.sisbec.beans.LibroPrestamoBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.forms.becario.PrestamoLibroForm;
import com.uca.sisbec.helpers.manto.LibroHelper;
import com.uca.sisbec.mapping.Becario;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.*;
/**
 *
 * @author Jorgore
 */
public class PrestamoLibroAction extends HibernateAction{
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
        if (request.getSession().getAttribute("librosList") == null)
            request.getSession().setAttribute("librosList", new ArrayList<LibroPrestamoBean>());
        request.setAttribute("agregar", "N");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward cargarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrestamoLibroForm plf = (PrestamoLibroForm) form;
        ActionErrors errors = new ActionErrors();
        LibroHelper helper = new LibroHelper(getSession(request));
        if (helper.fillFormFromIsbn(plf)){
            request.setAttribute("agregar", "Y");
        }else {
            if (Utils.isNullOrEmpty(plf.getLibro())){
                request.setAttribute("abrirBusqueda", "Y");
            } else {
                request.setAttribute("agregar", "N");
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.notAvailable", plf.getLibro()));
                request.setAttribute("errors", errors);
            }
        }
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward realizarPrestamo(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<LibroPrestamoBean> libros = (ArrayList<LibroPrestamoBean>)request.getSession().getAttribute("librosList");
        LibroHelper helper = new LibroHelper(getSession(request));
        Becario becario = getBecarioInSession(request);
        String carnet = becario.getCarnet();
        helper.registrarPrestamo(libros, carnet);
        request.getSession().removeAttribute("librosList");
        return mapping.findForward("pendientes");
    }
    
    public ActionForward agregarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrestamoLibroForm plf = (PrestamoLibroForm) form;
        LibroHelper helper = new LibroHelper(getSession(request));
        ActionErrors errors = new ActionErrors();
        Becario becario = getBecarioInSession(request);
        String carnet = becario.getCarnet();
        List<LibroPrestamoBean> libros = (ArrayList<LibroPrestamoBean>)request.getSession().getAttribute("librosList");
        if (!libroInList(plf.getIsbn(), libros)){
            if (!helper.libroInBD(plf.getIsbn(), carnet)){
                LibroPrestamoBean bean = new LibroPrestamoBean(plf.getIsbn(), plf.getLibro(), plf.getFecDevolucion());
                bean.setIndex(libros.size());
                libros.add(bean);
            } else {
                request.setAttribute("agregar", "N");
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.existeEnBD", plf.getLibro()));
                request.setAttribute("errors", errors);
            }
        } else {
            request.setAttribute("agregar", "N");
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.existeEnLista", plf.getLibro()));
            request.setAttribute("errors", errors);
        }
        plf.setIsbn("");
        plf.setIsbnLibro("");
        plf.setLibro("");
        plf.setFecDevolucion("");
        request.getSession().setAttribute("librosList", libros);
        return init(mapping, plf, request, response);
    }
    
    public ActionForward descartarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrestamoLibroForm plf = (PrestamoLibroForm) form;
        List<LibroPrestamoBean> libros = (ArrayList<LibroPrestamoBean>)request.getSession().getAttribute("librosList");
        libros.remove(Integer.parseInt(request.getParameter("index")));
        for (int i = 0; i < libros.size(); i++)
            libros.get(i).setIndex(i);
        plf.setIsbn("");
        plf.setIsbnLibro("");
        plf.setLibro("");
        plf.setFecDevolucion("");
        request.getSession().setAttribute("librosList", libros);
        return init(mapping, plf, request, response);
    }

    private void setOptionsList(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("librosList", session.getAttribute("librosList"));
        request.setAttribute("librosListReq", session.getAttribute("librosList"));
    }

    private boolean libroInList(String isbn, List<LibroPrestamoBean> libros) {
        for (int i = 0; i < libros.size(); i++){
            if (isbn.equals(libros.get(i).getIsbn()))
                return true;
        }
        return false;
    }
}
